﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;

namespace TabStrip {
    [ToolboxData("<{0}:Expando runat=server></{0}:Expando>")]
    [ParseChildren(ChildrenAsProperties=false)]
    [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
    public class Expando : Panel, INamingContainer {
    #region ViewState Storage for Properties
        private const string EXPANDO_IS_EXPANDED_PROP = "Expando_IsExpanded";
        private const string EXPANDO_CAPTION_PROP = "Expando_Caption";
        private const string EXPANDO_EXPANDED_CAPTION_CSSCLASS_PROP = "Expando_ExpandedCaptionCssClass";
        private const string EXPANDO_COLLAPSED_CAPTION_CSSCLASS_PROP = "Expando_CollapsedCaptionCssClass";
        private const string EXPANDO_CONTENTS_CSSCLASS_PROP = "Expando_ContentsCssClass";

        public bool IsExpanded {
            get {
                return ViewState[EXPANDO_IS_EXPANDED_PROP] == null ?
                    true :
                    (bool)ViewState[EXPANDO_IS_EXPANDED_PROP];
            }
            set {
                ViewState[EXPANDO_IS_EXPANDED_PROP] = value;
            }
        }

        public string Caption {
            get {
                return ViewState[EXPANDO_CAPTION_PROP] == null ?
                    "" :
                    (string)ViewState[EXPANDO_CAPTION_PROP];
            }
            set {
                ViewState[EXPANDO_CAPTION_PROP] = value;
            }
        }

        public string ExpandedCaptionCssClass {
            get {
                return ViewState[EXPANDO_EXPANDED_CAPTION_CSSCLASS_PROP] == null ?
                    "" :
                    (string)ViewState[EXPANDO_EXPANDED_CAPTION_CSSCLASS_PROP];
            }
            set {
                ViewState[EXPANDO_EXPANDED_CAPTION_CSSCLASS_PROP] = value;
            }
        }

        public string CollapsedCaptionCssClass {
            get {
                return ViewState[EXPANDO_COLLAPSED_CAPTION_CSSCLASS_PROP] == null ?
                    "" :
                    (string)ViewState[EXPANDO_COLLAPSED_CAPTION_CSSCLASS_PROP];
            }
            set {
                ViewState[EXPANDO_COLLAPSED_CAPTION_CSSCLASS_PROP] = value;
            }
        }

        public string ContentsCssClass {
            get {
                return ViewState[EXPANDO_CONTENTS_CSSCLASS_PROP] == null ?
                    "" :
                    (string)ViewState[EXPANDO_CONTENTS_CSSCLASS_PROP];
            }
            set {
                ViewState[EXPANDO_CONTENTS_CSSCLASS_PROP] = value;
            }
        }
    #endregion

        protected override void RenderContents(HtmlTextWriter writer) {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
                // caption
                writer.AddAttribute("id", ClientID + "_caption");
                writer.AddAttribute("class", IsExpanded ? ExpandedCaptionCssClass : CollapsedCaptionCssClass);
                writer.AddAttribute("onclick", "expandCollapse('" + ClientID + "','" + ExpandedCaptionCssClass + "','" + CollapsedCaptionCssClass + "')");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.Write(HttpUtility.HtmlEncode(Caption));
                writer.RenderEndTag();

                // contents
                writer.AddAttribute("id", ClientID + "_contents");
                writer.AddAttribute("class", ContentsCssClass);
                writer.AddStyleAttribute("display", IsExpanded ? "block" : "none");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    RenderChildren(writer);
                writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }
}
